<?php
 require_once("../inc/sku.php");
 require_once("../inc/inventory.php");
 
 $sku_results = getSKU_all();

?>
<!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title><?=$title ?> Product List</title>
         <? require('../inc/header-info.php'); ?>
    </head>
    <body>
        <div class="container">
            <? include('../inc/nav.php'); ?>
            <? if(isset($_GET['inserted'])): ?>
            	<div class="alert alert-success">
                	<button class="close">x</button>
                    	New SKU Added.
                </div>
            <? endif; ?>
            
            <? if(isset($_GET['updated'])): ?>
            	<div class="alert alert-success">
                	<button class="close">x</button>
                    	SKU Updated.
                </div>
            <? endif; ?>
            
            <? if(isset($_GET['deleted'])): ?>
            	<div class="alert alert-block">
                	<button class="close">x</button>
                    	SKU Deleted.
                </div>
            <? endif; ?>
            
            <a class="new-link btn btn-mini btn-primary" href="<?=$url?>sku/addsku.php">Add New Product</a>
			<table class="table table-striped">
    			<caption><h4>NP Commerce Product List</h4></caption>
    			<thead>
    				<tr>
    					<th>SKU</th>
    					<th>Vendor</th>
    					<th>Product Name</th>
    					<th>Description</th>
    					<th>Stock</th>
    					<th>Weight</th>
    					<th>Options</th>
    				</tr>
    			</thead>
    			<tbody>
    				
    				<? while($sku_row = $sku_results->fetch_assoc()): ?>
    					<tr>							
							<td><?=$sku_row['id']?></td>
							<td><?=$sku_row['vendor']?></td>
							<td><?=$sku_row['product_name']?></td>
							<td><?=$sku_row['description']?></td>
							<?
								$stock = getInventory($sku_row['id']);
								
								if($stock == null)
								{
									$stock = 0;	
								}
							?>
							<td><?=$stock ?></td>
							<td><?=$sku_row['weight']?></td>
                            <td>
                            	<a class="details-link btn btn-mini btn-primary" href="<?=$url?>sku/details.php?id=<?=$sku_row['id']?>">Details</a>
                            	<a class="edit-link btn btn-mini btn-primary" href="<?=$url?>sku/addsku.php?id=<?=$sku_row['id']?>">Edit</a>
                            	<a class="delete-link btn btn-mini btn-primary" href="<?=$url?>sku/delete.php?id=<?=$sku_row['id']?>">Delete</a>
                            </td>
                        </tr>
                    <? endwhile; ?>

    			</tbody>
    		</table>
		</div>
		
		<div id="modal-com" class="modal hide fade">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
					&times;
				</button>
				<h3 class='link-type'></h3>
			</div>
			<div class="modal-body">

			</div>
			<div class="modal-footer"></div>
		</div>

	<script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
		<script>
			$(function() {

				// Calls the New Modal
				$(".new-link").click(function(){
					$.get(this.href, function(data){
						$(".modal-body").html($(data).find("form"));
					})
					$(".link-type").append("New Product");
					$('#modal-com').modal('show');
					
					return false;
				});


				// Calls the Edit Modal
				$(".edit-link").click(function(){
					$.get(this.href, function(data){
						$(".modal-body").html($(data).find("form"));
					})
					$(".link-type").append("Edit Product");
					$('#modal-com').modal('show');
					
					return false;
				});
		
				// Calls the Delete Modal
				$(".delete-link").click(function(){
					$.get(this.href, function(data){
						$(".modal-body").html($(data).find("form"));
					})
					$(".link-type").append("Delete Confirmation");
					$('#modal-com').modal('show');
					
					return false;
				});
				
				//Post the Form Back to the server
				$("form").submit(function() {
					$("form").validate();
					var postData = $(this).serialize();
					$.post(this.action. postData + '&ajax=true', function(data){
						
					});
					
				});
				
				//Function to close and clear the modal
				$(".close").click(function(){
					$(".modal-body").empty();
					$(".link-type").empty();
					$('#modal-com').modal('hide');
					return false;
				});
				
				//---- Close the alert with the X
				$(".close").click(function(){
					$(this).closest(".alert").slideUp();
				});
	
				//---- Auto Close the Insert Confirmation Alert
				<? if(isset($_GET['inserted'])): ?>
					$(".alert").delay(2000).slideUp();
				<? endif; ?>
				
				//---- Auto Close the Update Confirmation Alert
				<? if(isset($_GET['updated'])): ?>
					$(".alert").delay(2000).slideUp();
				<? endif; ?>
				
				//---- Auto Close the Delete Confirmation Alert
				<? if(isset($_GET['deleted'])): ?>
					$(".alert").delay(2000).slideUp();
				<? endif; ?>
			});
		</script>
	</body>
</html>